package com.yincheng.oes.sso;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yincheng.oes.business.util.ExcelUtils;
import com.yincheng.oes.business.util.ExportExcel;

public class OrderHandleScript {

	public static void main(String[] args) throws FileNotFoundException, Exception {
		String rootFilePath = "D:\\银城教育\\双十一活动资料\\数据导出";
		Date date = new Date(); 
		String path=rootFilePath+new SimpleDateFormat("yyyy/MM/dd/").format(date);
		//如果不存在,创建文件夹
		File rootFile = new File(path);
		if(!rootFile.exists()){
			rootFile.mkdirs(); 
		}

		//读取excel
		File outFile = new File("C:\\Users\\Administrator\\Desktop\\加盟商所属订单.xlsx");
		String fileName = "加盟商所属订单.xlsx";
		//按照客服---加盟商的层级过滤数据
		String[] titles = {"订单号","订单创建时间","买家付款时间","付款方式","商品金额合计","运费","应收订单金额","订单实付金额","全部商品名称",
				"商品种类数","收货人/提货人","收货人手机号/提货人手机号","收货人省份","收货人城市","收货人地区","详细收货地址/提货地址","买家备注","买家姓名",
				"买家手机号","city_name","加盟商","客服老师","是否已回访","是否已开赠送课程","补款方式"};

		//获取数据
		List<List<Object>> listByExcel = ExcelUtils.getListByExcel(new FileInputStream(outFile), fileName);
		String customerServiceTmp = null;//客服临时变量
		String franchiseeTmp = null;//加盟商临时变量
		List<List<Object>> listFranchisee = new ArrayList<List<Object>>();//每个加盟商的数据集合临时变量
		String customerServicePath = path + "/" + listByExcel.get(0).get(21);
		File customerServiceFile = new File(customerServicePath);

		for (List<Object> row : listByExcel) {//row是每一行数据
			Object customerService = row.get(21);
			Object franchisee = row.get(20);
			if(customerService.toString().equals(customerServiceTmp)) {//判断这一行的客服是不是和之前的一样，一样则继续判断加盟商，不一样则创建新的文件夹
				if(franchisee.toString().equals(franchiseeTmp)) {//判断这一行的加盟商是否和上一行一样，一样则添加进数据行中，不一样则导出之前的数据成Excel，再重置数据集合
					listFranchisee.add(row);
				}else {
					generateExcel(titles, franchiseeTmp, listFranchisee, customerServiceFile);
					//将新加盟商订单数据添加进集合中
					listFranchisee.add(row);
				}
				customerServiceTmp = (String) customerService;
				franchiseeTmp = (String) franchisee;
			}else {
				//切换客服老师时，先生成之前加盟商的Excel文件
				if(franchiseeTmp != null) {
					generateExcel(titles, franchiseeTmp, listFranchisee, customerServiceFile);
				}
				//创建客服老师文件夹
				customerServicePath = path + "/" + customerService;
				customerServiceFile = new File(customerServicePath);
				if(!customerServiceFile.exists()){
					customerServiceFile.mkdirs(); 
				}
				listFranchisee.add(row);
				customerServiceTmp = (String) customerService;
				franchiseeTmp = (String) franchisee;
			}
		}
		
		generateExcel(titles, franchiseeTmp, listFranchisee, customerServiceFile);
		System.out.println("do-ne");
		
	}

	private static void generateExcel(String[] titles, String franchiseeTmp, List<List<Object>> listFranchisee,
			File customerServiceFile) throws FileNotFoundException, Exception, IOException {
		OutputStream out;
		String franchiseeName = "加盟商-"+franchiseeTmp + ".xls";
		out = new FileOutputStream(customerServiceFile + "\\" + franchiseeName);
		new ExportExcel(titles, listFranchisee).export(out);
		out.close();
		//清空临时数据集合listFranchisee
		listFranchisee.clear();
	}
}
